#include "depotview.h"
#include <QDebug>
#include <QItemSelectionModel>
#include <QStandardItemModel>
#include <QStringListModel>
#include <QDragEnterEvent>
#include <QMimeData>
#include "stock/view.h"
#include "depothandler.h"
#include "trade/tradehandler.h"

DepotView::DepotView(QObject *parent) :
    QObject(parent)
{
    qDebug()<<"Erstelle QStringListModel";

    depotNameModel=new QStringListModel(parent);
    stockNameModel=new QStringListModel(parent);

    depotNameView=new QListView();
    stockTableView=new DropTable();
}

DepotView::~DepotView()
{
}

void DepotView::attachStockNameListView(DragList *view)
{
    stockNameView=view;
    stockNameView->setModel(stockNameModel);        //attach the model
}

void DepotView::attachDepotNameListView(QListView *view)
{
    depotNameView=view;                             //take control of depot name view
    depotNameView->setModel(depotNameModel);        //attach the model

    /*When a depot name was clicked*/
    connect(depotNameView,SIGNAL(clicked(QModelIndex)),this,SLOT(depotNameClicked(QModelIndex)));
}

void DepotView::attachStockTableView(DropTable *view)
{
    stockTableView=view;                    //take control of the stock table
    stockTableView->setAcceptDrops(true);   //accept drops

    /*When an item was dropped on the table*/
    connect(stockTableView,SIGNAL(itemDropped(QDropEvent*)),this,SLOT(tableItemDropped(QDropEvent*)));
}

void DepotView::attachStandardLabels(QLabel *liquidityLabel, QLabel *standardProvisionLabel, QLabel *currentLiquidityLabel)
{
    this->liqLabel=liquidityLabel;
    this->stdProvLabel=standardProvisionLabel;
    this->cLiqLabel=currentLiquidityLabel;
}

void DepotView::attachTradeTable(QTableView *table)
{
    if(table)
        tradeTable=table;
}

/*setup the depot view and fill widgets with values*/
void DepotView::show()
{
    qDebug()<<"Setting up depot view: "<<getAllDepotNames();

    depotNameModel->setStringList(getAllDepotNames());          //fill the model with all depot names
    depotNameView->show();                                      //show the depot list

    stockNameModel->setStringList(Global::getStockInstance()->getStockNames());             //fill model with all stock names
    stockNameView->show();
}

/*SLOT: When a depot name was clicked*/
void DepotView::depotNameClicked(QModelIndex index)
{
    qDebug()<<"Clicked: "<<index<<" : "<<getAllDepotNames().at(index.row());
    //if(!currentDepotName.isEmpty())
      //  getDepot(currentDepotName)->disruptTradeTable();                        //disconnect the old trade model

    currentDepotName=getAllDepotNames().at(index.row());                        //set the current depot name to the name, which was clicked

    Depot* tmpDepot=getDepot(currentDepotName);                                 //load the depot with the clicked depot name

    connect(tmpDepot,SIGNAL(standardValueChanged(QString,QString,QString)),this,SLOT(refresh()));

    TradeHandler* tmpTradeHandler=tmpDepot;                                     //get the tradehandler of the loaded depot

    if(tmpTradeHandler)                                                         //if the tradehandler is set
    {
        tmpTradeHandler->attachTradeTable(tradeTable);                          //attach the table, where all trades of the depot will be shown
        tmpTradeHandler->showTradeTable();                                      //show all depot trades
    }

    tmpDepot->getStocks()->show(stockTableView);                                //show the stock values of the stocks in the loaded depot
    liqLabel->setText(tmpDepot->getStandardValues().value(DEPOT_TABLE_LIQUIDITY_FIELD)->data().toString());
    stdProvLabel->setText(tmpDepot->getStandardValues().value(DEPOT_TABLE_STANDARDPROVISION_FIELD)->data().toString());
    cLiqLabel->setText(tmpDepot->getStandardValues().value(DEPOT_TABLE_CURRENT_LIQUIDITY_FIELD)->data().toString());
}

/*slot called when a item was dropped on the stock table*/
void DepotView::tableItemDropped(QDropEvent *event)
{
    qDebug()<<"Table item dropped";
    addStockToDepot(currentDepotName,event->mimeData()->text(),NEW_DEPOT_STOCK);     //add stock name
}

DropTable* DepotView::getDropTable()
{
    return stockTableView;
}

void DepotView::refresh()
{
    /*refresh the current liquidity label*/
    cLiqLabel->setText(getDepot(currentDepotName)->getStandardValues().value(DEPOT_TABLE_CURRENT_LIQUIDITY_FIELD)->data().toString());
}

/*if a new stock is globally added, refresh the stock list in the depot view*/
void DepotView::newStockValue()
{
    stockNameModel->setStringList(Global::getStockInstance()->getStockNames());
}
